package com.example.dzbsh.service;

import com.example.dzbsh.dao.JiashuMapper;
import com.example.dzbsh.model.*;
import com.example.dzbsh.util.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class JiashuService {

    @Resource
    JiashuMapper ddMapper;
    @Resource
    JingshangQingkuangService ddJingshangQingkuangService;
    @Resource
    ChaheQingkuangService ddChaheQingkuangService;
    @Resource
    ZhenbieQingkuangService ddZhenbieQingkuangService;

    public int deleteByRenyuanId(String ppRenyuanId){
        JiashuExample mmExample = new JiashuExample();
        JiashuExample.Criteria criteria = mmExample.createCriteria();
        criteria.andRenyuanidEqualTo(ppRenyuanId);
        return ddMapper.deleteByExample(mmExample);
    }

    public List<Map<String, Object>>  findBydeptId(String ppDeptId){
        return ddMapper.findBydeptId(ppDeptId);
    }

    public Jiashu  findOne(String ppJiashuId){
        return ddMapper.selectByPrimaryKey(ppJiashuId);
    }

    public List<Map<String, Object>>  findByRenyuanid(String ppRenyuanId){
        return ddMapper.findByRenyuanid(ppRenyuanId);
    }
    public List<Map<String, Object>> findByRenyuanidByGuifan(String pprenyuanID){
        return ddMapper.findByRenyuanidByGuifan(pprenyuanID);
    }

    public List<Map<String, Object>>  findByDeptIdAndName(String ppDeptId,String ppJiashuName,int ppStartIndex, int ppPageSize){
        int mmStartIndex = (ppStartIndex - 1) * ppPageSize;
        return ddMapper.findByDeptIdAndName(ppDeptId,ppJiashuName,mmStartIndex,ppPageSize);
    }

    public Integer countByDeptIdAndName(String ppDeptId,String ppJiashuName) {
        Object mmCount = ddMapper.countByDeptIdAndName(ppDeptId,ppJiashuName);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }

    public List<Jiashu> findByShengfenzhenghao(String ppShengfenzhenghao){
        JiashuExample mmExample = new JiashuExample();
        JiashuExample.Criteria criteria = mmExample.createCriteria();
        criteria.andShenfenzhenghaoEqualTo(ppShengfenzhenghao);
        return ddMapper.selectByExample(mmExample);
    }

    public Integer getCountByisvalid() {
        Object mmCount = ddMapper.getCountByzhuangtai();
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }

    public Map<String, Object> findJingshangchaheduibiByDeptIdAndName(String ppDeptId,String ppJiashuName,int ppStartIndex, int ppPageSize){
        String mmJiashuname = "%"+ppJiashuName+"%";
        List<Map<String,Object>> mmList = findByDeptIdAndName(ppDeptId,mmJiashuname,ppStartIndex,ppPageSize);
        List<Map<String,Object>> mmResultList = new ArrayList<>();
        Map<String,Object> mmReturnMap = new HashMap<>();
        for (int i = 0; i < mmList.size(); i++) {
            Map<String,Object> mmMap = mmList.get(i);
            if (mmMap!=null){
                String mmJiashuId = mmMap.get("jiashuid")== null ?"":mmMap.get("jiashuid").toString();
                int mmChaheCount = ddChaheQingkuangService.countByJiashuId(mmJiashuId);
                int mmJingshangCount = ddJingshangQingkuangService.countByJiashuId(mmJiashuId);
                String mmShifouquanbubangding = "";

                List<ChaheQingkuang> mmWeibangdingChaheList = ddChaheQingkuangService.findByJiashuIdAndJingshangIdNull(mmJiashuId);
                if (mmWeibangdingChaheList.size()>0){
                    mmShifouquanbubangding="否";
                }else {
                    mmShifouquanbubangding="是";
                }

                String mmShifouYujingshangyizhi = "是";

                List<ChaheQingkuang> mmYujingshangbuyizhiList = ddChaheQingkuangService.findByJiashuIdAndYujingshangbuyizhi(mmJiashuId);
                if (mmYujingshangbuyizhiList.size()>0){
                    mmShifouYujingshangyizhi="否";
                }

                mmMap.put("chahecount",mmChaheCount);
                mmMap.put("jingshangcount",mmJingshangCount);
                mmMap.put("shifouquanbubangding",mmShifouquanbubangding);
                mmMap.put("shifouyujingshangyizhi",mmShifouYujingshangyizhi);

                mmResultList.add(mmMap);
            }
        }
        Integer mmCount = countByDeptIdAndName(ppDeptId,mmJiashuname);
        PageInfo mmPageInfo = new PageInfo(ppStartIndex, ppPageSize, mmCount);

        mmReturnMap.put("JiashuList", mmResultList);
        mmReturnMap.put("PageInfo", mmPageInfo);
        return mmReturnMap;
    }
    public Map<String, Object> findZhenbieqingkuangByDeptIdAndName(String ppDeptId,String ppJiashuName,int ppStartIndex, int ppPageSize){
        String mmJiashuname = "%"+ppJiashuName+"%";
        List<Map<String,Object>> mmList = findByDeptIdAndName(ppDeptId,mmJiashuname,ppStartIndex,ppPageSize);
        List<Map<String,Object>> mmResultList = new ArrayList<>();
        Map<String,Object> mmReturnMap = new HashMap<>();
        for (int i = 0; i < mmList.size(); i++) {
            Map<String,Object> mmMap = mmList.get(i);
            if (mmMap!=null){
                String mmJiashuId = mmMap.get("jiashuid")== null ?"":mmMap.get("jiashuid").toString();
                int mmZhenbieCount = ddZhenbieQingkuangService.countByJiashuId(mmJiashuId);
                mmMap.put("zhenbiecount",mmZhenbieCount);

                mmResultList.add(mmMap);
            }
        }
        Integer mmCount = countByDeptIdAndName(ppDeptId,mmJiashuname);
        PageInfo mmPageInfo = new PageInfo(ppStartIndex, ppPageSize, mmCount);

        mmReturnMap.put("JiashuList", mmResultList);
        mmReturnMap.put("PageInfo", mmPageInfo);
        return mmReturnMap;
    }

}
